package com.hamilton.erik.pt.mobile.android.app;

import java.util.Calendar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.hamilton.erik.pt.core.util.DateTimeUtil;
import com.hamilton.erik.pt.mobile.android.R;

public class DateTimePickerDialog extends AlertDialog implements 
	OnClickListener, OnDateChangedListener, OnTimeChangedListener {
		
	public interface OnDateTimeSetListener {
		void onDateTimeSet(DatePicker datePicker, TimePicker timePicker,
							int year, int monthOfYear, int dayOfMonth,
							int hourOfDay, int minute);
	}
	
	private final static String YEAR = "year";
	private final static String MONTH = "month";
	private final static String DAY = "day";
	private final static String HOUR = "hour";
	private final static String MINUTE = "minute";
	
	private OnDateTimeSetListener callBack;
	
	private DatePicker datePicker;
	private TimePicker timePicker;
	
	private Calendar titleCalendar;
	
	public DateTimePickerDialog(Context context, OnDateTimeSetListener callBack,
								int year, int month, int dayOfMonth,
								int hourOfDay, int minute) {
		super(context);
		
		this.callBack = callBack;

		this.titleCalendar = Calendar.getInstance();
		
		setButton(context.getText(R.string.set_button_text), this);
		setButton2(context.getText(R.string.cancel_button_text),(OnClickListener)null);
		
		LayoutInflater inflater = 
				(LayoutInflater) context.getSystemService(
											Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.date_time_picker_dialog, null);
		setView(view);
		
		datePicker = (DatePicker) view.findViewById(R.id.date_picker);
		datePicker.init(year, month, dayOfMonth, this);

		timePicker = (TimePicker) view.findViewById(R.id.time_picker);
		timePicker.setCurrentHour(hourOfDay);
		timePicker.setCurrentMinute(minute);
		timePicker.setOnTimeChangedListener(this);
		timePicker.setIs24HourView(false);
		
		updateTitle();
	}

	public void onClick(DialogInterface dialog, int which) {
		if(callBack != null) {
			datePicker.clearFocus();
			timePicker.clearFocus();
			callBack.onDateTimeSet(datePicker, timePicker, 
					datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
					timePicker.getCurrentHour(), timePicker.getCurrentMinute());
		}
	}
		
	@Override
	public Bundle onSaveInstanceState() {
		Bundle state = super.onSaveInstanceState();
		state.putInt(YEAR, datePicker.getYear());
		state.putInt(MONTH, datePicker.getMonth());
		state.putInt(DAY, datePicker.getDayOfMonth());
		state.putInt(HOUR, timePicker.getCurrentHour());
		state.putInt(MINUTE, timePicker.getCurrentMinute());
		return state;
	}
	
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		
		datePicker.init(
				savedInstanceState.getInt(YEAR),
				savedInstanceState.getInt(MONTH),
				savedInstanceState.getInt(DAY),
				null);
		timePicker.setCurrentHour(savedInstanceState.getInt(HOUR));
		timePicker.setCurrentMinute(savedInstanceState.getInt(MINUTE));
	}

	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		updateTitle();
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		updateTitle();
	}
	
	private void updateTitle() {
		titleCalendar.set(Calendar.YEAR, datePicker.getYear());
		titleCalendar.set(Calendar.MONTH, datePicker.getMonth());
		titleCalendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
		titleCalendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
		titleCalendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
		
		setTitle(
				DateTimeUtil
					.dateToStandardDateTimeString(
								titleCalendar.getTime()));
	}
}
